package communicationMessages;

import java.util.Date;
import java.util.logging.Level;

import logger.LoggerWriter;

import data.Forum;
import data.Member;
import data.MemberDataBase;
import data.SubForum;

public class AddPostMessage extends Message {

	@Override
	public String getAnswer() {
		String[] arr = this.content.split("\n");
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		String ans = "";
		if (arr.length != 6)
		{
			logger.log("failed\nadd tpost Msg wrong format\n", Level.WARNING);
			ans = "failed\nadd tpost Msg wrong format\n";
		}
		else
		{
			String userName,subject,body,subForumId,threadMsgId,responseToMsgId;
			userName = arr[0];
			subject = arr[1];
			body = arr[2];
			subForumId = arr[3];
			threadMsgId = arr[4];
			responseToMsgId = arr[5];
			MemberDataBase tUserDataBase =MemberDataBase.getMembersDataBase();
			Member user = tUserDataBase.getMember(userName);
			Forum forum = Forum.getForum();
			SubForum subForum = forum.getSubForum(Integer.valueOf(subForumId));
			if (subForum == null){
				logger.log("subForum is null the number is " + subForumId , Level.WARNING);
				ans = "failed\nsubForum wasn't found\n";
				return ans;
			}
			if (subForum.addPostMsg(subject, body, user, new Date(System.currentTimeMillis()), Integer.valueOf(threadMsgId), Integer.valueOf(responseToMsgId)))
			{
				ans="addPostMessage\nsuccess\n";
			}
			else 
			{
				ans="addPostMessage\nfailed\n";
			}
			
		}
		return ans;
	}

}
